import constant from '@/store/constant.js'
import common from '@/store/common.js'
const baseUrl=constant.baseUrl;
const authInfo=constant.authInfo;

export default {
	state:{//存放状态
		todoItem:{},
		doingList:[],
		doneList:[],
		
		
		
	},
	mutations: {
		
		loadTodoData(state,data){
			state.doneList=[]
			state.doingList=[]
			for(let index in data.doing){
				state.doingList.push(data.doing[index])
			}
			for(let index in data.done){
				state.doneList.push(data.done[index])
			}
			
			// if(data.flag==1){
			// 	state.todoList=[]
			// }
			// console.log(state.shareList.length)
			// for(let index in data.data){
			// 	state.shareList.push(data.data[index])
			// }
			// console.log(state.shareList.length)
			
		},
		
		
	},
	actions:{
		
		loadTodoData(context,data){
			
			console.log("loadTodoData"+JSON.stringify(data))
			uni.request({
				url: baseUrl+'/api/todo/listForGroup',
				method: 'POST',
				header: {
				        'auth':uni.getStorageSync("token") 
				    },
				data: {},
				success: (res) => {
					if(res.data.code==0){
					
						context.commit('loadTodoData',res.data.result)
						
					}else{
						common.dataError(res.data);
						console.log(res.data.message);
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
			
		},
		addTodoData(context,data){
			
			var url="";
			
			if(data.id.length>0){
				url=baseUrl+"/api/todo/editTodo";
			}else{
				url=baseUrl+"/api/todo/addTodo";
			}		
			console.log(uni.getStorageSync("token") )
			console.log(JSON.stringify(data));
			uni.request({
				url: url,
				method: 'POST',
				header: {
				        'auth': uni.getStorageSync("token") 
				    },
				data:data,
				success: (res) => {
					console.log(JSON.stringify(res.data))
					if(res.data.code==0){
						context.dispatch('loadTodoData',{})
						
					}else{
						common.dataError(res.data);
						console.log(res.data.message);
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		},
		
	}
}